<template>
  <!-- //文章详情页面 -->
  <div class="article-detail">
    <!-- 导航栏部分 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章部分 -->
    <div class="article-wrap">
      <h3 class="title">我是一个标题</h3>
      <div class="aut_info">
        <van-image
          width="60"
          height="60"
          fit="cover"
          src="https://img01.yzcdn.cn/vant/cat.jpeg"
          round
        />
        <div class="info">
          <div>作者名</div>
          <div class="time">1年前</div>
        </div>
        <!-- 如果用户是作者，即id=id会true显示，便写！=，！=则是fasle不显示  -->
        <div v-if="$store.state.userInfo.id != info.aut_id">
          <van-button
            type="info"
            round
            v-if="!info.is_followed"
            @click="doFollow(true)"
            >+ 关注</van-button
          >
          <van-button type="danger" round v-else @click="doFollow(false)"
            >- 取关</van-button
          >
        </div>
      </div>

      <!-- 文章内容 --><!-- 文章内容 --><!-- 文章内容 --><!-- 文章内容 --><!-- 文章内容 -->
      <div class="content" v-html="info.content">我是内容</div>
      <!-- 分割线 -->
      <van-divider>end</van-divider>
      <!-- 功能按钮区域 -->
      <div class="btn-wrap">
        <van-button
          type="primary"
          plain
          round
          icon="good-job-o"
          @click="zan(true)"
          v-if="info.attitude == -1"
          >点赞</van-button
        >

        <van-button
          style="width: 150px"
          type="primary"
          plain
          round
          icon="good-job"
          @click="zan(false)"
          v-if="info.attitude == 1"
          >取消点赞</van-button
        >
        <div style="width: 30px" v-if="info.attitude == -1"></div>
        <van-button
          type="danger"
          plain
          round
          icon="delete-o"
          style="width: 150px"
          v-if="info.attitude == -1"
          @click="unlike(true)"
          >不喜欢</van-button
        >
        <van-button
          type="danger"
          plain
          round
          icon="delete"
          style="width: 150px"
          v-if="info.attitude == 0"
          @click="unlike(false)"
        >
          取消不喜欢</van-button
        >
      </div>
    </div>

    <!-- 评论部分 -->
    <comment />
  </div>
</template>

<script>
import {
  articleInfoAPI,
  unfollowUserAPI,
  followUserAPI,
  zanAPI,
  unzanAPI,
  unlikeAPI,
  cancelUnlikeAPI,
} from "@/api"; //articleInfoAPI文章详情
import comment from "./comment";
export default {
  name: "article1",
  data() {
    return {
      info: {}, //created之初请求返回的数据

      status: "",
    };
  },
  components: {
    comment,
  },
  async created() {
    let res = await articleInfoAPI(this.$route.query.id);
    this.info = res.data.data;
    // console.log(this.info);
  },
  methods: {
    async doFollow(status) {
      this.info.is_followed = status;
      try {
        if (status) {
          //status两种状态true/false
          //关注
          await followUserAPI({
            target: this.info.aut_id, //用户的id//需要以对象传进去
          });
          this.$toast.success("关注成功");
        } else {
          //取消关注
          await unfollowUserAPI(
            this.info.aut_id //用户的id//这个是指不用花括号
          );
          this.$toast.success("取消成功");
        }
        // this.$toast.success("操作成功");
      } catch {
        this.$toast.fail("操作失败，请联系管理员"); //13911111111的账号是作者本人，通过控制台network查看followings显示自己不能关注自己
      }
    },

    //跟赞有关
    async zan(status) {
      if (status) {
        await zanAPI({
          target: this.$route.query.id,
        });
        this.info.attitude = 1;
      } else {
        await unzanAPI(this.$route.query.id);
        this.info.attitude = -1;
      }
    },

    //跟不喜欢有关
    async unlike(status) {
      if (status) {
        await unlikeAPI({
          target: this.$route.query.id,
        });
        this.info.attitude = 0;
      } else {
        await cancelUnlikeAPI(this.$route.query.id);
        this.info.attitude = -1;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.article-detail {
  //文章部分
  .article-wrap {
    padding: 0 20px;
    .title {
      font-size: 25px;
      margin: 10px 0;
    }
    .aut_info {
      height: 80px;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      .info {
        margin-left: 20px;
        // 设置剩余空间里的占比，如果就只给它设置了
        // 就代表剩余部分全部给它
        flex: 1;

        .time {
          color: gray;
        }
      }
    }
    .btn-wrap {
      display: flex;
      justify-content: center;
      .van-button {
        width: 120px;
      }
    }
  }

  //文章内容
  .content {
    overflow: hidden;
    // word-break: break-all; 以一行实在放不下了就换行可能会导致单词脱节
    // 如果最后一个单词不够放了，就把整个单词换一行开始写
    // word-wrap: break-word;
    //  设置空格换行那些格式，保持原样
    // pre:会保留空格，但我想它与pre-line/pre-wrap最大的不同点就是不会自动换行，单行走到底，直到遇到一个换行符才会换行。
    //white-space: pre这个不会换行的，记得不要写成这个//这个部分第六天有修改
    white-space: pre-wrap;
    // scoped 还要加::v-deep 不然文章内容背景没颜色了 因为<pre><code>都不是这个页面的标签
    ::v-deep {
      pre {
        code {
          display: block;
          width: 100%;
          overflow: auto;
          background-color: #101010;
          color: #dcdcdc;
        }
      }
      img {
        width: 80%;
      }
    }
  }
}
</style>
